package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class ProjectPeer extends BasePeer{
	public static final String tableName = "Project";
	public static final String PROJECTID = "projectId";
	public static final String NAME = "name";
	public static final String[] columnNames=new String[]{"projectId","name"};
	public static Project getProjectByPKs(Object projectId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(ProjectPeer.tableName,ProjectPeer.columnNames," TRUE  AND `projectId`=? ",new Object[]{projectId,},0, 1,conn);
			Project project = new Project();
		fill(rs,project);
		return project;
	}
	public static Project getProjectReferenceColumnsOnlyByPKs(Object projectId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(ProjectPeer.tableName,new String[]{"projectId",}," TRUE AND `projectId`=?",new Object[]{projectId,}, 0,  1,conn);
			Project project = new Project();
		fill(rs,project);
		return project;
	}
	public static Project getProjectByProjectId(long projectId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(ProjectPeer.tableName,ProjectPeer.columnNames,"`projectId`=?",new Object[]{projectId},0, 1,conn);
			Project project = new Project();
		fill(rs,project);
		return project;
	}
	public static void fill(ResultSet rs, Project project) throws SQLException {
		if(rs.next()){
			try{project.setProjectId(rs.getLong("projectId"));}catch(Exception ex){}
			try{project.setName(rs.getString("name"));}catch(Exception ex){}
			project.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<Project> projectCollection) throws SQLException {
		while(rs.next()){
			Project project=new Project();
			try{project.setProjectId(rs.getLong("projectId"));}catch(Exception ex){}
			try{project.setName(rs.getString("name"));}catch(Exception ex){}
			project.resetModified();
			projectCollection.add(project);
		}
	}
	public static boolean update(Project project, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < ProjectPeer.columnNames.length; i++) {
			if (((ProjectBase)project).modified[i]) {
				columns.add(ProjectPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `Project` SET ");
		sql.append("WHERE TRUE AND `projectId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,project.get(column));
		}
		pstmt.setObject(columns.size()+1+0,project.get("projectId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(Project project, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (project.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `Project` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (project.get(column) != null) {
				pstmt.setObject(i + 1 + j, project.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<Project> getProjectCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<Project> collection=new Vector<Project>();
		fill(rs,collection);
		return collection;
	}
}
